package com.education.platform.common.service.impl;


import com.education.platform.common.dao.BaseDao;
import com.education.platform.common.dao.entity.BaseDo;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.BaseService;
import com.education.platform.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

/**
 * service基类
 * @param <D>
 * @param <T>
 */
public abstract class BaseServiceImpl<D extends BaseDao<T>,T extends BaseDo> implements BaseService<T> {

    @Autowired
    protected D dao;


    @Override
    public List<T> getList(T dto) {
        List list = dao.getList(dto);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(T dto) {
        return dao.insert(dto)>0;
    }

    @Override
    public boolean insertInBatch(List<T> dtoList) {
        return dao.insertInBatch(dtoList)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(T dto) {
        if(dao.updateById(dto)!=1){
            throw new BusinessException("更新失败");
        }
        return true;
    }

    @Override
    public boolean updateInBatch(List<T> dtoList) {
        return dao.updateInBatch(dtoList)>0;
    }

    @Override
    public boolean deleteById(T dto) {
        return dao.deleteById(dto)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(List<Integer> ids) {
        if(dao.deleteByIds(ids)==ids.size()){
            return true;
        }
        throw new BusinessException("删除失败");
    }




    @Override
    public T get(T dto) {
        return dao.get(dto);
    }

    @Override
    public T getById(Integer pk) {
      return   dao.getById(pk);
    }

    @Override
    public List<T> getListByIds(List<Integer> pks) {
        return dao.getListByIds(pks);
    }


}
